/**
 *
*/
package com.bbcare.util.nullCheck;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.util.FieldNotNull;

/**
 * @Description: TODO
 * @author MLi
 * @date 2018年4月9日
 * @version V1.0
 */
public class IntergerNull extends ObjectNull {

	@Override
	public void isNullCheck(Method method, Object obj, FieldNotNull fieldNotNull, String nestFieldName) {
		Integer result = null;
		try {
			result = (Integer) method.invoke(obj);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		if(result == null){
			try{
				throw (new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
                    Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "为空！"));
			} catch (AppException e) {
				throw(e);
			}
		} else {
			int minValue = fieldNotNull.minIntValue();
			if(result < minValue){
				throw new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "最小值超出范围！");
			}
			int maxValue = fieldNotNull.maxIntValue();
			if(result > maxValue){
				throw new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "最大值超出范围！");
			}
			int[] intValues = fieldNotNull.intValues();
			if(intValues.length != 0 && !contains(intValues, result)){
				throw new AppException(ErrorCode.DISTREAT_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, Constants.API_INPUT_PARAMS_DATA_ERROR + "，" + fieldNotNull.desc() + "取值非法！");
			}
		}
	}

	/**
	 * @param intValues
	 * @param result
	 * @return
	 */
	private boolean contains(int[] intValues, Integer result) {
		for (int i : intValues) {
			if(i == result){
				return true;
			}
		}
		return false;
	}

}
